package com.xxx.meter.base;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.xxx.meter.app.AppManager;
import com.xxx.meter.ui.main.view.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {

    private ImmersionBar mImmersionBar;
    private LoadingDialog loadingDialog;
    private Unbinder bind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        //添加到AppManager中
        AppManager.getInstance().addActivity(this);

        //初始化ButterKnife
        bind = ButterKnife.bind(this);

        //初始化加载框
        loadingDialog = LoadingDialog.getInstance(this);

        //沉浸式状态栏
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();

        //设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        //初始化数据
        initData();
    }

    @Override
    public void finish() {
        super.finish();
        AppManager.getInstance().finishActivity(this);
    }

    @Override
    protected void onDestroy() {
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
            mImmersionBar = null;
        }
        if (bind != null) {
            bind.unbind();
            bind = null;
        }
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        super.onDestroy();
    }

    public void showLoading() {
        if (loadingDialog != null && !isFinishing() && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    public void hideLading() {
        if (loadingDialog != null && !isFinishing() && loadingDialog.isShowing()) {
            loadingDialog.hide();
        }
    }

    //获取到Layout的ID
    protected abstract int getLayoutId();

    //初始化数据
    protected abstract void initData();

}
